<template>
	<view class="container">
		<template v-if="empty">
		    <view class="tabsContent">
				<u-empty mode="data" width="280" height="280" textSize="28"
		     icon="http://cdn.uviewui.com/uview/empty/data.png" text="您未添加访客记录"></u-empty>
		    </view>
		</template>
		<template v-else>
			<view class="formBox" v-for="(item,index) in list">
				<view @click="toVisitorDetails(item)">
					<view class="visitor-num">访客登记{{index+1}}</view>
					<view class="line">
						<view class="line-title">访客姓名：</view>
						<u--text :size="26" color="#999999" mode="name" :text="item.visitName"></u--text>
					</view>
					<view class="line">
						<view class="line-title">联系电话：</view>
						<u--text :size="26" color="#999999" mode="phone" :text="item.phonenumber"></u--text>
					</view>
					<view class="line">
						<view class="line-title">访客时间：</view>
						<u--text :size="26" color="#999999" :text="item.startTime"></u--text>
					</view>
					<view class="line">
						<view class="line-title">地址：</view>
						<u--text :size="26" color="#999999" :text="item.address"></u--text>
					</view>
				</view>
				<u-gap height="16"></u-gap>
				<view class="line line-btn">
					<u-button type="primary" style="width: 40%; height: 100%;" @click="editVisitor(item)" :plain="true" text="编辑"></u-button>
					<u-button type="error" style="width: 40%; height: 100%;" :plain="true" text="删除" @click="delVisitor(item,index)"></u-button>
				</view>
			</view>
		</template>
		<view style="width:100% ;height:110rpx ;">
			
		</view>
		<view class="bottomBtn">
			<u-button @click="addVisitor" type="primary" text="添加访客信息"></u-button>
		</view>
		<u-modal :show="deleteShow" :title="deleteTitle" showCancelButton buttonReverse :zoom="false"
			:closeOnClickOverlay="true" @confirm="deleteConfirm" @cancel="deleteShow = false"
			@close="deleteShow = false "></u-modal>
	</view>
</template>

<script>
	import api from '@/api/api.js';
	export default {
		name: '',
		components: {},
		data() {
			return {
				pageNum:1,
				pageSize:3,
				list:[],
				num:null,//条数
				empty:false,//是否为空
				deleteShow: false,
				deleteTitle: '是否确认删除',
				deleteId: null,
			}
		},
		onShow(){
			const _that=this
			_that.pageNum=1
			_that.list=[]
			_that.num=null
			_that.empty=false
			_that.deleteShow=false
			_that.getVisitorList()
		},
		created() {
			
		},
		mounted() {},
		destroyed() {},
		methods: {
			// 获取公告列表
			getVisitorList(){
				let userInfo = uni.getStorageSync('userInfo')
				let data={
					pageNum:this.pageNum,
					pageSize:this.pageSize,
					visitUserId: userInfo.id
				}
				api.getVisitor(data).then(res=>{
					if(res.data.code==601){
						this.empty=true
					}else if(res.data.code==200){
						this.list=this.list.concat(res.data.rows)
						this.num=Math.ceil(res.data.total/this.pageSize)//总条数除一页多少条向上取整
						console.log(this.num)
						this.pageNum++
					}
				})
			},
			// 新增访客信息
			addVisitor(){
				uni.$u.route('/pages/visitor/addVisitor');
			},
			// 编辑访客信息
			editVisitor(item){
				uni.$u.route({
					type:"navigateTo",
					url: "/pages/visitor/addVisitor",
					params: {
						id: item.id
					}
				});
			},
			// 删除访客信息
			delVisitor(item,index){
				this.deleteShow = true;
				this.deleteTitle = `是否确认删除 访客信息${index+1}`
				this.deleteId = item.id
			},
			deleteConfirm() {
				this.deleteShow = false;
				api.delVisitor(this.deleteId).then(res => {
					if (res.data.code == 200) {
						this.pageNum=1
						this.list=[]
						this.num=null
						this.getVisitorList()
						setTimeout(() => {
							uni.$u.toast('删除成功')
						}, 300)
					}
				}).catch(err => {
					uni.$u.toast(err.msg)
				})
			},
			// 跳转访客详情
			toVisitorDetails(item){
				uni.$u.route({
					type:"navigateTo",
					url: "/pages/visitor/details",
					params: {
						id: item.id
					}
				});
			}
		},
		// 触底加载更多
		onReachBottom() {
			// 触底的时候请求数据，即为上拉加载更多
			if(this.pageNum<=this.num){
				this.getVisitorList()
			}
		},
		// 下拉刷新
		onPullDownRefresh() {
			const _that=this
			_that.pageNum=1
			_that.list=[]
			_that.num=null
			_that.empty=false
			_that.deleteShow=false
			setTimeout(function () {
				uni.stopPullDownRefresh();
				_that.getVisitorList();
			}, 1000);
		}
	}
</script>

<style lang="scss" scoped>
	.container{
		width: calc(100% - 32rpx);
		padding: 16rpx;
		.tabsContent {
			width: 100%;
			background-color: #FFFFFF;
			height: 1000rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}
		.formBox {
			width: calc(100% - 64rpx);
			padding: 16rpx 32rpx;
			border-radius: 8rpx;
			background-color: #FFFFFF;
			margin: 16rpx 0;
			.visitor-num{
				font-size: 28rpx;
				font-weight: bold;
				margin-bottom: 10rpx;
			}
			.line-btn{
				justify-content: space-around;
			}
			.line {
				display: flex;
				width: 100%;
				height: 60rpx;
				line-height: 60rpx;
		
				.line-title {
					width: 200rpx;
					font-size: 28rpx;
		
				}
			}
		}
		.bottomBtn{
			position: fixed;
			width: calc(100% - 63rpx) ;
			padding: 32rpx;
			bottom: 0;
			left: 0;
			background-color: #FFFFFF;
			display: flex;
			align-items: center;
			justify-content: center;
			box-shadow: 0rpx 1rpx 8rpx 0rpx #e5e5e5;
		}
	}
</style>
